#import "SEGCommunityLocationView.h"

#if __has_include(<Commom/SEGLayoutMacro.h>)
#import <Commom/SEGLayoutMacro.h>
#else
#import "SEGLayoutMacro.h"
#endif

#if __has_include(<Commom/SEGColorManager.h>)
#import <Commom/SEGColorManager.h>
#else
#import "SEGColorManager.h"
#endif

#if __has_include(<Commom/YYBaseHeader.h>)
#import <Commom/YYBaseHeader.h>
#else
#import "YYBaseHeader.h"
#endif

#if __has_include(<Commom/YYBaseHeader.h>)
#import <Commom/YYBaseHeader.h>
#else
#import "YYBaseHeader.h"
#endif

#import <Masonry/Masonry.h>

@implementation SEGCommunityLocationViewModel
-(instancetype)init{
    if (self == [super init]) {
        _communityName = @"";
    }
    return self;
}
@end

@implementation SEGCommunityLocationViewLayout
- (instancetype)initWithData:(NSString *)communityName type:(SEGEnumCommunityLocationViewType)type{
    if (self == [super init]) {
        _communityName = communityName;
        _type = type;
    }
    [self segCommunityLocationViewLayout];
    return self;
}

-(void)segCommunityLocationViewLayout{
    //高度固定
    _totalHeight = kCommunityLocationViewHeight;
    if (_type == SEGEnumCommunityLocationViewType_NO) {
        _totalWidth = kCommunityLocationViewWidth;
    }if(_type == SEGEnumCommunityLocationViewType_LuNeng){
        _totalHeight = 45;
        _totalWidth = kCommunityLocationViewHasLocationWidth;
    } else{
        _totalWidth = kCommunityLocationViewHasLocationWidth;
    }
    
    //高度不固定
    //_totalHeight = 0;
    _communityLabelHeight = 0;
    _communityLabelTextLayout = nil;
    
    if (!_communityName) return;
    
    [self layout];
}

//数据处理
-(void)layout{
    
    UIFont *font = [UIFont fontWithName:@"Heiti SC" size:13];
    if (_type == SEGEnumCommunityLocationViewType_LuNeng) {
        font = [UIFont fontWithName:@"Heiti SC" size:14];
    }
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:_communityName];
    text.yy_font = font;
    UIColor *yyColor = kColor_W;
    if (_type == SEGEnumCommunityLocationViewType_LuNeng) {
        yyColor = kColor_W;
        text.yy_font = [text.yy_font fontWithBold];
    }
	if (_type == SEGEnumCommunityLocationViewType_NO) {
        yyColor = kColor_1;
    }
    text.yy_color = yyColor;
    
    SEGTextContainer *container = [SEGTextContainer new];
    container.size = CGSizeMake(LYScreenWidth() , HUGE);//* 0.298
    container.maximumNumberOfRows = 1;
    
    _communityLabelTextLayout = [SEGTextLayout layoutWithContainer:container text:text];
    
    CGFloat W = [SEGKitHelper seg_sizeWithString:_communityName fontSize:13 textColor:kColor_1].width;
    if (_type == SEGEnumCommunityLocationViewType_LuNeng) {
        W = [SEGKitHelper seg_sizeWithString:_communityName fontSize:14 textColor:kColor_W].width;
    }
   
    // 内容间距
    if (_type == SEGEnumCommunityLocationViewType_NO) {
        CGFloat tmp = W + kCommunityLocationViewMargin * 2;
        _totalWidth = (_totalWidth < tmp)?tmp :_totalWidth;
    }else if (_type == SEGEnumCommunityLocationViewType_LuNeng){
        CGFloat imgW = [UIImage imageWithContentsOfFile:SEGPicName(@"icon_location_address02")].size.width;
        CGFloat imgW2 = [UIImage imageWithContentsOfFile:SEGPicName(@"ic_popup_white01")].size.width;
        _totalWidth =  W + imgW + imgW2 + 10;
        _totalWidth += 20;
    }else{
        CGFloat imgW = [UIImage imageWithContentsOfFile:SEGPicName(@"icon_location_address02")].size.width;
        CGFloat tmp = W + imgW + kCommunityLocationViewMargin * 2 + kCommunityLocationViewLocationMargin;
        _totalWidth = (_totalWidth < tmp)?tmp :_totalWidth;
    }
}
@end

@implementation SEGCommunityLocationView
- (instancetype)initWithFrame:(CGRect)frame {
    if (frame.size.width == 0 && frame.size.height == 0) {
        frame.size.width = [UIScreen mainScreen].bounds.size.width;
        frame.size.height =  kCommunityLocationViewHeight;
    }
    self = [super initWithFrame:frame];
    
    [self addSubview:self.communityLabel];
    [self addSubview:self.locationImageView];
    [self addSubview:self.menuImageView];
    //self.backgroundColor = kSegRandomColor;
    
    self.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithActionBlock:^(UITapGestureRecognizer*  _Nonnull sender) {
        if (self.delegate && [self.delegate respondsToSelector:@selector(protocolcommunityLabel:type:)]) {
            [self.delegate protocolcommunityLabel:nil type:self.layout.type];
        }
    }];
    [self addGestureRecognizer:tap];
    return self;
}

-(void)setWithLayout:(SEGCommunityLocationViewLayout *)layout{
    self.height = layout.totalHeight;
    self.width = layout.totalWidth;
    _layout = layout;
    
    if (layout.type != SEGEnumCommunityLocationViewType_LuNeng){
        self.layer.masksToBounds = YES;
        self.layer.cornerRadius = 0.5 * kCommunityLocationViewHeight;
    }
    
    if (self.height <= 0) return;
    
    if(![self.communityLabel.textLayout isEqual:layout.communityLabelTextLayout]) self.communityLabel.textLayout = layout.communityLabelTextLayout;
    
    [self setupUI];
}

//界面布局
-(void)setupUI{
    if (_layout.type == SEGEnumCommunityLocationViewType_NO) {
        self.backgroundColor = [UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:0.5];
        self.locationImageView.image = [UIImage imageWithContentsOfFile:SEGPicName(@"icon_location_address02")];
        
        [self.communityLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(self.communityLabel.superview);
        }];
    }else if (_layout.type == SEGEnumCommunityLocationViewType_LuNeng){
        self.locationImageView.image = [UIImage imageWithContentsOfFile:SEGPicName(@"icon_location_address02")];
        [self.locationImageView sizeToFit];
        [self.locationImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.locationImageView.superview);
            make.left.equalTo(self.locationImageView.superview);
        }];
        [self.communityLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.locationImageView.superview);
            make.left.equalTo(self.locationImageView.mas_right).mas_offset(5);
        }];
        self.menuImageView.image = [UIImage imageWithContentsOfFile:SEGPicName(@"ic_popup_white01")];
        [self.menuImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.locationImageView.superview);
            make.left.equalTo(self.communityLabel.mas_right).mas_offset(5);
        }];
    }else{
        self.backgroundColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.5];
        self.locationImageView.image = [UIImage imageWithContentsOfFile:SEGPicName(@"icon_location_address")];
        
        [self.locationImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.locationImageView.superview);
            make.left.equalTo(self.locationImageView.superview).mas_offset(kCommunityLocationViewMargin);
        }];
        
        [self.communityLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.locationImageView.superview);
            make.left.equalTo(self.locationImageView.mas_right).mas_offset(kCommunityLocationViewLocationMargin);
        }];
    }
}

#pragma mark - getter
-(SEGLabel *)communityLabel{
    if (!_communityLabel) {
        _communityLabel = [[SEGLabel alloc]init];
        _communityLabel.userInteractionEnabled = YES;
        _communityLabel.textAlignment = NSTextAlignmentCenter;
        _communityLabel.textVerticalAlignment = SEGTextVerticalAlignmentCenter;
        //_communityLabel.displaysAsynchronously = YES;
        _communityLabel.ignoreCommonProperties = YES;
        _communityLabel.fadeOnHighlight = NO;
        _communityLabel.fadeOnAsynchronouslyDisplay = NO;
        __weak typeof(self) _self = self;
//        _communityLabel.textTapAction = ^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
//            if (_self.delegate && [_self.delegate respondsToSelector:@selector(protocolcommunityLabel:type:)]) {
//                [_self.delegate protocolcommunityLabel:_self.communityLabel type:_self.layout.type];
//            }
//        };
    }
    return _communityLabel;
}


-(UIImageView *)locationImageView{
    if (!_locationImageView) {
        _locationImageView = [[UIImageView alloc]init];
        _locationImageView.contentMode = UIViewContentModeScaleAspectFill;
    }
    return _locationImageView;
}

-(UIImageView *)menuImageView{
    if (!_menuImageView) {
        _menuImageView = [[UIImageView alloc]init];
        _menuImageView.contentMode = UIViewContentModeScaleAspectFill;
    }
    return _menuImageView;
}


@end

